<template>
	<view>
		<!-- 地址 -->
		<view class="address-section">
			<view class="order-content" @tap="jumpToPage('/pages/address/addressList')">
				<text class="icon-address cuIcon-location"></text>
				<view class="cen" v-if="address.addressId == '-1'">
					<view class="top">
						请选择上门地址
					</view>
				</view>
				<view class="cen" v-else>
					<view class="top">
						<text class="cu-tag light bg-red sm margin-right-sm" v-if="address.isDefault == 'T'">默认</text>
						<text class="name">{{address.username}}</text>
						<text class="mobile">{{address.tel}}</text>
					</view>
					<text class="address">{{ address.address }}</text>
				</view>
				<text class="cuIcon-right"></text>
			</view>
		</view>

		<view class="date-section flex-direction">
			<view class="flex justify-between">
				<text class="date-title">期望上门时间</text>
				<view class="date-select text-red" @click="showTimePanel()">{{appTime}}<text
						class="cuIcon-right"></text></view>
			</view>
			<text class="date-desc text-gray"><text class="cuIcon-infofill"></text>已为您推荐选择上门服务时间，点击可修改期望上门时间</text>
		</view>
		<view class="goods-section">
			<!-- 商品列表 -->
			<view class="g-item" v-for="(g,index) in perGoodsList" :key="g.index">
				<image :src="g.coverPhoto">
				</image>
				<view class="right">
					<text class="title clamp">{{ g.prodName || '' }}</text>
					<view class="price-box flex justify-between">
						<text class="price">{{g.price}}元起</text>
						<text class="number">x {{ g.goodsNum }}</text>
					</view>
					<text class="spec">上门确认服务价格后，可参与优惠活动</text>
				</view>
			</view>
		</view>

		<!-- 金额明细 -->
		<view class="yt-list">
			<view class="yt-list-cell desc-cell">
				<text class="remark margin-right">订单备注</text>
				<input class="desc" type="text" v-model="desc" placeholder="请填写备注信息" placeholder-class="placeholder" />
			</view>
		</view>
		<!-- 底部 -->
		<view class="footer">
			<view class="price-content">
				<text class="text-gray">合计:</text>
				<text class="price">{{ totalFee || '0.00' }}元起</text>
			</view>
			<text class="submit cu-btn bg-gradual-orange" @click="subOrder">提交订单</text>
		</view>
		<view class="mask" :class="showTime==false ? 'none' : 'show'" @click="toggleMask">
			<view class="mask-content" @click.stop.prevent="stopPrevent">
				<times @change="selectTime" disableText=""></times>
			</view>
		</view>
	</view>
</template>

<script>
	import times from '@/components/pretty-times/pretty-times.vue'

	import {
		formatTimeNow,
		formatIphoneDateTime
	} from '../../lib/java110/utils/DateUtil.js'
	import {
		getUserAddress
	} from "../../api/user/userApi"

	import {
		getStoreCart,
		goodsUnifieldOrder,
		toPayServ
	} from '../../api/order/orderApi.js'

	import {
		getHousekeepingServList
	} from "../../api/homemaking/homemakingApi"
	import {
		toPay
	} from '../../api/pay/payApi.js';

	import param from '../../constant/param.js'
	import conf from '@/conf/config.js';
	export default {
		components: {
			times
		},
		data() {
			return {
				desc: '', //备注
				payType: 1, //1微信 2支付宝
				appTime: formatTimeNow(),
				showTime: false, //时间面板显示状态
				address: {
					isDefault: 0,
					addressId: '-1',
					usernameL: '',
					tel: ''
				},
				addressId: 0,
				servId: '',
				shopId: '',
				cartId: '',
				personId: '',
				perGoodsList: [], //确认单订单商品
				totalFee: 0.0
			}
		},
	 async onLoad(options) {
		 await this.vc.onLoad(options).then(_suc=>{},_err=>{});
			
			if(conf.env == 'PROD'){				
				let openId = options.openId;
				console.log(openId)
				//校验 是否已经获取用户openId
				if(!openId) this.vc.hasUserOpenId();
			}			
			//商品数据
			this.getDefaultAddress();
			this.shopId = options.shopId;
			if (options.hasOwnProperty("cartId")) {
				this.cartId = options.cartId;
				this.getPre();
			} else {
				this.servId = options.servId;
				this.getServ();
			}
		},
		methods: {
			showTimePanel() {
				this.showTime = true
			},
			selectTime(e) {
				this.showTime = false
				this.appTime = formatIphoneDateTime(e)
			},
			// 初始地址
			getDefaultAddress: function() {
				let that = this;
				getUserAddress({
						userId: this.vc.getCurrentUser().userId,
						isDefault: 'T',
						page: 1,
						row: 1
					})
					.then((_data) => {
						if (_data.data != null && _data.data.length > 0) {
							that.address = _data.data[0]
						}
					})
			},
			numberChange(data) {
				this.number = data.number;
			},
			changePayType(type) {
				this.payType = type;
			},
			getPre: function() {
				let that = this;
				getStoreCart({
					personId: this.vc.getCurrentUser().userId,
					page: 1,
					row: 50,
					cartType: param.CART_TYPE.HOMEMAKING,
					cartId: that.cartId
				}).then(res => {
					if (res.code === 0 && res.data.length > 0) {
						let _data = res.data;
						_data.forEach(item => {
							that.perGoodsList.push({
								checked: true,
								productId: item.productId,
								valueId: item.valueId,
								goodsNum: item.cartNum,
								price: item.price,
								prodName: item.prodName,
								coverPhoto: item.coverPhoto,
								storeId: item.storeId,
								areaCode: item.areaCode,
								shopId: item.shopId,
								cartId: item.cartId,
								cartType: param.CART_TYPE.HOMEMAKING
							});

							that.totalFee = parseFloat(that.totalFee) + (parseFloat(item.cartNum) *
								parseFloat(item.price));
							that.goodsCount += 1;
						})

						that.totalFee = that.totalFee.toFixed(2)
					}
				});
			},
			getServ: function() {
				let that = this;
				getHousekeepingServList({
					page: 1,
					row: 1,
					servId: this.servId,
					// 大类查询，不用传
					shopId: this.shopId
				}).then(res => {
					if (res.code === 0 && res.data.length > 0) {
						let _data = res.data;
						_data.forEach(item => {
							that.perGoodsList.push({
								checked: true,
								productId: item.servId,
								valueId: item.servId,
								goodsNum: 1,
								price: item.defaultFee,
								prodName: item.servName,
								coverPhoto: item.coverPhoto,
								areaCode: item.areaCode,
								storeId: '',
								shopId: item.shopId,
								cartId: '-1',
								cartType: param.CART_TYPE.HOMEMAKING
							});
							that.totalFee = parseFloat(that.totalFee) + parseFloat(item.defaultFee);
							that.goodsCount += 1;
						})

						that.totalFee = that.totalFee.toFixed(2)
					}
				});
			},
			subOrder: function() {
				let that = this;
				wx.showLoading({
					title: '支付中'
				});
				that.perGoodsList.forEach(item => {
					item.appointmentTime = this.appTime;
				})
				let _data = this._getPayParam();
				toPay(_data, 'buyServ', this.shopId,this.totalFee,'/pages/homemaking/order?stat=2102');
			},
			stopPrevent: function() {

			},
			jumpToPage: function(_pageUrl) {
				this.vc.navigateTo({
					url: _pageUrl
				})
			},
			_getPayParam: function() {
				let _storeOrderCarts = [];
				let _that = this;
				this.perGoodsList.forEach(item => {
					if (item.checked) {
						_storeOrderCarts.push({
							"cateType": "1",
							"productId": item.productId,
							"prodName": item.prodName,
							"valueId": item.valueId,
							"goodsNum": item.goodsNum,
							"appointmentTime":item.appointmentTime,
							"shopId": _that.shopId,
							"cartType": param.CART_TYPE.HOMEMAKING
						})
					}
				});
				if (_storeOrderCarts.length < 1) {
					this.vc.toast({
						icon: 'none',
						title: '请选择商品'
					});
					return;
				}
				if(!_that.address.addressId){
					this.vc.toast({
						icon: 'none',
						title: '请选择地址'
					});
					return;
				}
				return {
					"storeOrder": {
						remark: _that.remark,
						personId: _that.vc.getCurrentUser().userId,
						personName: _that.vc.getCurrentUser().userName,
						userId: _that.vc.getCurrentUser().userId,
					},
					"storeOrderCarts": _storeOrderCarts,
					"shopId": _that.shopId,
					//"vipUsers": _that.orderInfo.vipUserDto,
					"addressId": _that.address.addressId,
				}
			},
		}
	}
</script>

<style lang="scss">
	page {
		background: $uni-bg-color-grey;
		padding-bottom: 100upx;
	}

	.address-section {
		padding: 30upx 0;
		background: #fff;
		position: relative;

		.order-content {
			display: flex;
			align-items: center;
		}

		.icon-address {
			flex-shrink: 0;
			display: flex;
			align-items: center;
			justify-content: center;
			width: 90upx;
			color: #888;
			font-size: 44upx;
		}

		.cen {
			display: flex;
			flex-direction: column;
			flex: 1;
			font-size: 28upx;
			color: $font-color-dark;
		}

		.name {
			font-size: 34upx;
			margin-right: 24upx;
		}

		.address {
			margin-top: 16upx;
			margin-right: 20upx;
			color: $font-color-light;
		}

		.cuIcon-right {
			font-size: 32upx;
			color: $font-color-light;
			margin-right: 30upx;
		}

		.a-bg {
			position: absolute;
			left: 0;
			bottom: 0;
			display: block;
			width: 100%;
			height: 5upx;
		}
	}

	.date-section {
		margin-top: 16upx;
		background: #fff;
		padding: 30upx;

		.date-select {
			font-weight: bold;
		}

		.date-desc {
			font-size: 22upx;
		}
	}

	.goods-section {
		margin-top: 16upx;
		background: #fff;
		padding-bottom: 1px;

		.logo {
			display: block;
			width: 50upx;
			height: 50upx;
			border-radius: 100px;
		}

		.name {
			font-size: 30upx;
			color: $font-color-base;
			margin-left: 24upx;
		}

		.g-item {
			display: flex;
			margin: 20upx 30upx;
			padding-top: 20upx;

			image {
				flex-shrink: 0;
				display: block;
				width: 140upx;
				height: 140upx;
				border-radius: 4upx;
			}

			.right {
				flex: 1;
				padding-left: 24upx;
				overflow: hidden;
			}

			.title {
				font-size: 30upx;
				color: $font-color-dark;
			}

			.spec {
				font-size: 26upx;
				color: $font-color-light;
			}

			.price-box {
				display: flex;
				align-items: center;
				font-size: 30upx;
				color: red;
				padding: 10upx 0;

				.price {
					margin-bottom: 4upx;
				}

				.number {
					font-size: 26upx;
					color: $font-color-base;
					margin-left: 20upx;
				}
			}

			.step-box {
				position: relative;
			}
		}
	}

	.yt-list {
		margin-top: 16upx;
		background: #fff;
	}

	.yt-list-cell {
		display: flex;
		align-items: center;
		padding: 10upx 30upx 10upx 40upx;
		line-height: 70upx;
		position: relative;

		&.cell-hover {
			background: #fafafa;
		}

		&.b-b:after {
			left: 30upx;
		}

		.cell-icon {
			height: 32upx;
			width: 32upx;
			font-size: 22upx;
			color: #fff;
			text-align: center;
			line-height: 32upx;
			background: #f85e52;
			border-radius: 4upx;
			margin-right: 12upx;

			&.hb {
				background: #ffaa0e;
			}

			&.lpk {
				background: #3ab54a;
			}

		}

		.cell-more {
			align-self: center;
			font-size: 24upx;
			color: $font-color-light;
			margin-left: 8upx;
			margin-right: -10upx;
		}

		.cell-tit {
			flex: 1;
			font-size: 26upx;
			color: $font-color-light;
			margin-right: 10upx;
		}

		.cell-tip {
			font-size: 26upx;
			color: $font-color-dark;

			&.disabled {
				color: $font-color-light;
			}

			&.active {
				color: $base-color;
			}

			&.red {
				color: $base-color;
			}
		}

		&.desc-cell {
			.cell-tit {
				max-width: 90upx;
			}
		}

		.desc {
			flex: 1;
			font-size: $font-base;
			color: $font-color-dark;
		}
	}

	/* 支付列表 */
	.pay-list {
		padding-left: 40upx;
		margin-top: 16upx;
		background: #fff;

		.pay-item {
			display: flex;
			align-items: center;
			padding-right: 20upx;
			line-height: 1;
			height: 110upx;
			position: relative;
		}

		.icon-weixinzhifu {
			width: 80upx;
			font-size: 40upx;
			color: #6BCC03;
		}

		.icon-alipay {
			width: 80upx;
			font-size: 40upx;
			color: #06B4FD;
		}

		.icon-xuanzhong2 {
			display: flex;
			align-items: center;
			justify-content: center;
			width: 60upx;
			height: 60upx;
			font-size: 40upx;
			color: $base-color;
		}

		.tit {
			font-size: 32upx;
			color: $font-color-dark;
			flex: 1;
		}
	}

	.footer {
		position: fixed;
		left: 0;
		bottom: 0;
		z-index: 995;
		display: flex;
		align-items: center;
		width: 100%;
		height: 90upx;
		justify-content: space-between;
		font-size: 30upx;
		background-color: #fff;
		z-index: 998;
		color: $font-color-base;
		box-shadow: 0 -1px 5px rgba(0, 0, 0, .1);

		.price-content {
			padding-left: 30upx;
		}

		.price-tip {
			color: $base-color;
			margin-left: 8upx;
		}

		.price {
			font-size: 36upx;
			color: $base-color;
		}

		.submit {
			display: flex;
			align-items: center;
			justify-content: center;
			width: 280upx;
			height: 100%;
			color: #fff;
			font-size: 32upx;
			background-color: $base-color;
		}
	}

	/* 优惠券面板 */
	.mask {
		display: flex;
		align-items: flex-end;
		position: fixed;
		left: 0;
		top: 0;
		bottom: 0;
		width: 100%;
		background: rgba(0, 0, 0, 0);
		z-index: 9995;
		transition: .3s;

		.mask-content {
			width: 100%;
			height: 100%;
			background: #f3f3f3;
			transform: translateY(100%);
			transition: .3s;
			overflow-y: scroll;
		}

		&.none {
			display: none;
		}

		&.show {
			background: rgba(0, 0, 0, .4);

			.mask-content {
				transform: translateY(0);
			}
		}
	}

	/* 优惠券列表 */
	.coupon-item {
		display: flex;
		flex-direction: column;
		margin: 20upx 24upx;
		background: #fff;

		.con {
			display: flex;
			align-items: center;
			position: relative;
			height: 120upx;
			padding: 0 30upx;

			&:after {
				position: absolute;
				left: 0;
				bottom: 0;
				content: '';
				width: 100%;
				height: 0;
				border-bottom: 1px dashed #f3f3f3;
				transform: scaleY(50%);
			}
		}

		.left {
			display: flex;
			flex-direction: column;
			justify-content: center;
			flex: 1;
			overflow: hidden;
			height: 100upx;
		}

		.title {
			font-size: 32upx;
			color: $font-color-dark;
			margin-bottom: 10upx;
		}

		.time {
			font-size: 24upx;
			color: $font-color-light;
		}

		.right {
			display: flex;
			flex-direction: column;
			justify-content: center;
			align-items: center;
			font-size: 26upx;
			color: $font-color-base;
			height: 100upx;
		}

		.price {
			font-size: 44upx;
			color: $base-color;

			&:before {
				content: '￥';
				font-size: 34upx;
			}
		}

		.tips {
			font-size: 24upx;
			color: $font-color-light;
			line-height: 60upx;
			padding-left: 30upx;
		}

		.circle {
			position: absolute;
			left: -6upx;
			bottom: -10upx;
			z-index: 10;
			width: 20upx;
			height: 20upx;
			background: #f3f3f3;
			border-radius: 100px;

			&.r {
				left: auto;
				right: -6upx;
			}
		}
	}
</style>
